package cn.easybuy.dao.impl;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.UserDAO;
import cn.easybuy.entity.PageInfo;
import cn.easybuy.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.easybuy.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDao implements UserDAO {


    @Override
    public int addUser(User u) throws SQLException {
        String sql="insert into easybuy_user(id,loginName,userName,password,sex,identityCode,email,mobile,type) values(?,?,?,?,?,?,?,?,?)";
        Object[]obj = new Object[9];
        obj[0]=u.getId();
        obj[1]=u.getLoginName();
        obj[2]=u.getUserName();
        obj[3]=u.getPassword();
        obj[4]=u.getSex();
        obj[5]=u.getIdentityCode();
        obj[6]=u.getEmail();
        obj[7]=u.getMobile();
        obj[8]=u.getType();

        int i = executeUpdate(sql,obj);

        return i;
    }

    @Override
    public int delUsers(User u) throws SQLException {
        String sql="delete from easybuy_user where id=?";
        Object[] obj = new Object[1];
        obj[0]=u.getId();

        int i = executeUpdate(sql, obj);

        return i;
    }

    @Override
    public int updUsers(User u) throws SQLException {
        String sql="update easybuy_user set loginName=?,userName=?,password=?,sex=?,identityCode=?,email=?,mobile=?,type=? where id=?";
        Object [] obj = new Object[9];
        obj[0] = u.getLoginName();
        obj[1]=u.getUserName();
        obj[2]=u.getPassword();
        obj[3]=u.getSex();
        obj[4]=u.getIdentityCode();
        obj[5]=u.getEmail();
        obj[6]=u.getMobile();
        obj[7]=u.getType();
        obj[8] =u.getId();

        int i = executeUpdate(sql, obj);

        return i;
    }

    @Override
    public List<User> getListAll() throws SQLException {
        List<User>list=new ArrayList<>();
        String sql="select * from easybuy_user";
        ResultSet rs = executeQuery(sql, new Object[0]);
        while (rs.next()){
            int id = rs.getInt("id");
            String loginName = rs.getString("loginName");
            String userName = rs.getString("userName");
            String password = rs.getString("password");
            int sex = rs.getInt("sex");
            String identityCode = rs.getString("identityCode");
            String email = rs.getString("email");
            String mobile = rs.getString("mobile");
            int type = rs.getInt("type");
            User u = new User(id,loginName,userName,password,sex,identityCode,email,mobile,type);
            list.add(u);
        }

        return list;
    }

    @Override
    public PageInfo<User> listPage(PageInfo<User> page) throws Exception {
        String sql="select * from easybuy_user limit ?,?";

        Object[] obj = new Object[2];
        obj[0]=(page.getPageNo()-1)*page.getPageSize();//当前页
        obj[1]=page.getPageSize();//每页显示的条数

        ResultSet rs =executeQuery(sql,obj);
        ArrayList<User>list=new ArrayList<>();
        while (rs.next()){
            int id = rs.getInt("id");
            String loginName = rs.getString("loginName");
            String userName = rs.getString("userName");
            String password = rs.getString("password");
            int sex = rs.getInt("sex");
            String identityCode = rs.getString("identityCode");
            String email = rs.getString("email");
            String mobile = rs.getString("mobile");
            int type = rs.getInt("type");
            User u = new User(id,loginName,userName,password,sex,identityCode,email,mobile,type);
            //把数据添加到list集合
            list.add(u);
        }
        page.setPageList(list);

        //总记录条数 总页数
        String sqlCount="select count(*) total  from easybuy_user";
        ResultSet rs2 =executeQuery(sqlCount,null);
        int count=0;
        if (rs2.next()){
            count=rs2.getInt("total");
        }

        //计算总页数
        int size =page.getPageSize();
        //count 是总记录条数, size是每页显示的条数 num是条数
        int num=count%size==0?count/size : count/size+1;

        page.setPageNum(num);
        page.setPageCount(count);

        return page;
    }
    // 注册功能
    @Override
    public int AddNews(User user) throws SQLException {
        String sql = "insert into easybuy_user(id,loginName,userName,password,sex,identityCode,email,mobile,type) values(null,?,?,?,?,?,?,?,?)";
        Object[] obj = new Object[8];
        obj[0] = user.getLoginName();
        obj[1] = user.getUserName();
        obj[2] = user.getPassword();
        obj[3] = user.getSex();
        obj[4] = user.getIdentityCode();
        obj[5] = user.getEmail();
        obj[6] = user.getMobile();
        obj[7] = user.getType();
        int i = executeUpdate(sql, obj);
        return i;
    }
    // 重复验证
    @Override
    public User selectUserInfo(User user) throws SQLException {
        String sql = "select * from easybuy_user where email =? or mobile =?";
        Object[] obj = new Object[2];
        obj[0] = user.getEmail();
        obj[1] = user.getMobile();
        ResultSet rs = executeQuery(sql, obj);
        User u = null;
        while (rs.next()){
            String email = rs.getString("email");
            String phone = rs.getString("mobile");
            u = new User();
            u.setEmail(email);
            u.setMobile(phone);
        }
        return u;
    }
    // 登录功能
    @Override
    public User checkLogin(User user) throws SQLException {
        String sql = "select * from easybuy_user where loginName =? and password =?";
        Object[] obj = new Object[2];
        obj[0] = user.getLoginName();
        obj[1] = user.getPassword();
        ResultSet rs = executeQuery(sql, obj);
        User u = null;
        while (rs.next()){
            int id = rs.getInt("id");
            String loginName = rs.getString("loginName");
            String userName = rs.getString("userName");
            String password = rs.getString("password");
            int sex = rs.getInt("sex");
            String email = rs.getString("email");
            String mobile = rs.getString("mobile");
            int type = rs.getInt("type");
            u = new User();
            u.setUserName(userName);
            u.setSex(sex);
            u.setEmail(email);
            u.setMobile(mobile);
            u.setId(id);
            u.setLoginName(loginName);
            u.setPassword(password);
            u.setType(type);
        }
        return u;
    }
}
